package ir;

public class EnumDataNode extends DataNode {

  public EnumDataNode(Class<Object> dataClass) {
    super(dataClass);
    
  }

  /**
   * Generate class for this data type: all getters, setters, and private fields.
   * 
   * For Enums such as this, we ASSUME a private String Value field.
   */
  @Override
  public String gen() {
    
    String generated = "";
    
    // generate field code
    for (MemberNode n : fieldNodes)
      if (!n.gen().equals(""))
        generated = "  " + n.gen() + generated;
    
    
    // generate method code
    for (MemberNode n : methodNodes)
      if (!n.gen().equals(""))
        generated += "  " + n.gen();
    
    String toStringMethod = getToStringMethod();
    
    // generate imports
    String imports = getImports();
    
    String blank_constructor = INDENT + "public " + getGeneratedClassName() + "() {}\n\n";
    String constructor = INDENT + "public " + getGeneratedClassName() + "(String Value) { this.Value = Value; } \n";
    String value_declare = "private String Value;\n";
    generated = imports + "\n" + "public class " + getGeneratedClassName() + 
      " implements Serializable { \n" + value_declare + blank_constructor + constructor + generated + " \n } \n";
    
    generated = getPackageDeclaration() + generated;
    
    return generated + "\n";
  }
  
  private String getToStringMethod() {
    return "public String toString() { return Value; }";
  }

  public String generateGwt2Soap() {
    // <Datatype.name> Gwt2Soap ( Gwt<Datatype.Name> in ) {
    //      <DataType.name> out = <Datatype.name>.fromValue(in.toString())
    //      return out;
    //  }
    
    String name = underlier.getSimpleName();
    String method_head = INDENT + "public " + name + " " + "Gwt2Soap" + " (" + getGeneratedClassName() + " in) {\n" +
      INDENT + INDENT + name + " out = " + name + ".fromValue(in.getValue());";
    
    String method_body = "";
    
    method_body += INDENT + INDENT + "return out;\n";
    
    method_body += INDENT + "}\n\n";
    
    
    
    return method_head + method_body;
      
  }
  
  public String generateSoap2Gwt() {
    // Gwt<Datatype.name> Soap2Gwt ( <Datatype.Name> in ) {
    //      Gwt<DataType.name> out = new Gwt<DataType.name>();
    //      out.set<FIELD> ( in.get<FIELD> )
    //              ... etc.
    //      return out;
    //  }
    
    String underlierName = underlier.getSimpleName();
    
    String method_head = INDENT + "public " + getGeneratedClassName() + " " + "Soap2Gwt" + " (" + underlierName + " in) {\n" +
      INDENT + INDENT + getGeneratedClassName() + " out = new " + getGeneratedClassName() + "(in.getValue());\n";
    
    String method_body = "";
    
    method_body += INDENT + INDENT + "return out;\n";
    
    method_body += INDENT + "}\n\n";
    
    return method_head + method_body;
      
  }
  
  /**
   * AdData[] -> GwtAdData[]
   * foreach AdData, GwtAdData[i] = Soap2Gwt(AdData)
   * @return
   */
  public String generateArraySoap2Gwt() {

    // input: AdData
    String arr_in = getUnderlier().getSimpleName() + "[]";
    String method_args = "(" + arr_in + " in)";
    
    // form the method sig
    // Return: GwtAdData[]
    String method_return_type = getGeneratedClassName() + "[]";
    String method_head = "public " + method_return_type + " Soap2Gwt" + method_args;
    
    
    // form method logic of unpacking, translating, repacking
    String arr_out = method_return_type + " out";
    String declare_arr_out = arr_out + " = new " + getGeneratedClassName() + "[in.length];\n";

    String packer = "for (int i = 0; i < in.length; i++) { out[i] = Soap2Gwt( in[i] ); }\n";
    String returning = "return out;\n";
    String method_body = declare_arr_out + packer + returning;
    
    String method = method_head + "{\n" + method_body + "\n}\n";
    
    return method;
  }
  
  /**
   * GwtAdData[] -> AdData[]
   * foreach GwtAdData, AdData[] = Gwt2Soap(GwtAdData)
   * @return
   */
  public String generateArrayGwt2Soap() {
 // input: AdData
    String arr_in = getGeneratedClassName() + "[]";
    String method_args = "(" + arr_in + " in)";
    
    // form the method sig
    // Return: GwtAdData[]
    String method_return_type = getUnderlier().getSimpleName() + "[]";
    String method_head = "public " + method_return_type + " Gwt2Soap" + method_args;
    
    
    // form method logic of unpacking, translating, repacking
    String arr_out = method_return_type + " out";
    String declare_arr_out = arr_out + " = new " + getUnderlier().getSimpleName() + "[in.length];\n";

    String packer = "for (int i = 0; i < in.length; i++) { out[i] = Gwt2Soap( in[i] ); }\n";
    String returning = "return out;\n";
    String method_body = declare_arr_out + packer + returning;
    
    String method = method_head + "{\n" + method_body + "\n}\n";
    
    return method;
  }


}
